﻿@{
    ViewBag.Title = "留样配置管理";
}

<div class="row col-md-12 ml-0 vh-3">
    <strong class="font-rfs">留样配置管理</strong>
</div>
<div class="row col-md-12 ml-0 min-vh-90">
    <div class="row col-md-12 min-vh-90" style="padding-right:0">
        <div class="row col-md-12 vh-5">
            <button id="addRetention" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
            <button id="editRetention" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
            <button id="delRetention" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
        </div>
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 min-vh-85 pre-scrollable">
            <table id="tableRetention" class="table table-striped table-bordered table-sm table-hover"></table>
        </div>
    </div>
</div>
<div class="modal" id="SAMPLE-AddRetention-Add" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="AddRetention-ModalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="SAMPLE-AddRetention-ModalID" style="display:none">SAMPLE-AddRetention-Add</label>
            </div>
            <div class="modal-body">
                <div id="AddRetention-Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="SAMPLE-AddRetention-Delete" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中留样配置吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="Delete-Modal-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="Delete-Modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var RetentionId = "";
        $(document).ready(function () {
            LoadRetentionConfig();
        });

        function LoadRetentionConfig() {
            var columns = [{ field: "id", visible: false }, { field: "name", title: "名称" }, { field: "container", title: "包装" },
            { field: "containerNumber", title: "包装数量" }, { field: "quantity", title: "每包数量" }, { field: "unit", title: "单位" },
            { field: "duration", title: "留存时长" }, { field: "durationUnit", title: "留存单位" }, { field: "remind", title: "提醒" },
            { field: "position", title: "存储位置" }, { field: "storageConditions", title: "存储条件" },
            { field: "disApproval", title: "处审", width: "5%", align: "center", formatter: SetdisApproval },
            { field: "useApproval", title: "领审", width: "5%", align: "center", formatter: SetuseApproval }];

            RetentionId = "";  /*清空该值*/
            $('#tableRetention').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tableRetention').bootstrapTable({
                method: 'post',
                url: "GetRetentionConfigList",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset // 偏移量
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 10,   // 默认页大小
                pageList: [10, 15, 20, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,
                onClickRow: function (row, $element, field) {
                    $("#tableRetention").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    RetentionId = row.id;
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Runtime-ResultEntry-Table').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        };

        function SetdisApproval(value, row, index) {
            var strHtml = "";
            if (row.disApproval) {
                strHtml = "<input type='checkbox' checked onclick='UpdateDisApproval(this,event)'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='UpdateDisApproval(this,event)'/>";
            }
            return strHtml;
        }

        function SetuseApproval(value, row, index) {
            var strHtml = "";
            if (row.useApproval) {
                strHtml = "<input type='checkbox' checked onclick='UpdateUseApproval(this,event)'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='UpdateUseApproval(this,event)'/>";
            }
            return strHtml;
        }

        function UpdateDisApproval(obj) {
            var rowid = $(obj).parent().parent().attr('data-uniqueid');

            $.post("UpdateRetentionCfDisApproval", {
                RetentionConfigId: rowid
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
            event.stopPropagation();
        }

        function UpdateUseApproval(obj) {
            var rowid = $(obj).parent().parent().attr('data-uniqueid');

            $.post("UpdateRetentionCfUseApproval", {
                RetentionConfigId: rowid
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
            event.stopPropagation();
        }

        $("#addRetention").click(function () {
            $("#AddRetention-ModalTitle").text("添加留样配置");

            $("#SAMPLE-AddRetention-Add").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddRetentionConfig", {
                RetentionConfigId: "",
                ModalID: "SAMPLE-AddRetention-Add"
            }, function (datas) {
                $("#AddRetention-Embed").html(datas);
            });
        });

        $("#editRetention").click(function () {
            if (!RetentionId) {
                alert("请先选择留样配置！");
                return;
            }

            $("#AddRetention-ModalTitle").text("编辑留样配置");

            $("#SAMPLE-AddRetention-Add").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddRetentionConfig", {
                Id: RetentionId,
                ModalID: "SAMPLE-AddRetention-Add"
            }, function (datas) {
                $("#AddRetention-Embed").html(datas);
            });
        });

        $("#delRetention").click(function () {
            if (!RetentionId) {
                alert("请先选择留样配置！");
                return;
            }

            $("#SAMPLE-AddRetention-Delete").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#Delete-Modal-delete").click(function () {
            $.post("DelRetentionConfig", {
                Id: RetentionId
            }, function (datas) {
                alert(datas);
                $("#SAMPLE-AddRetention-Delete").modal('hide');
                LoadRetentionConfig();
            });
        });

        $('#SAMPLE-AddRetention-Add').on('hidden.bs.modal', function (event) {
            $.post("GetRetentionConfig", {
                Id: RetentionId
            }, function (datas) {
                var row = $('#tableRetention').bootstrapTable('getRowByUniqueId', RetentionId);
                row.name = datas.name;
                row.containerNumber = datas.containerNumber;
                row.container = datas.container;
                row.quantity = datas.quantity;
                row.unit = datas.unit;
                row.duration = datas.duration;
                row.durationUnit = datas.durationUnit;
                row.remind = datas.remind;
                row.position = datas.position;
                row.storageConditions = datas.storageConditions;
                $('#tableRetention').bootstrapTable('updateByUniqueId', row);
            });
        });

    </script>
}
